﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class CommentEditControl : YuanlinUserControl,
        IReplyConverter<CommentReply, GetOperation>
    {
        protected ValuedEditor descriptionValuedEditor;
        protected ValuedTextBox newsIdValuedTextBox;
        protected ValuedTextBox entityTypeValuedTextBox;
        protected ValuedTextBox createDateValuedTextBox;
        protected ValuedTextBox stateValuedTextBox;
        protected ValuedTextBox userNameValuedTextBox;
        protected IButton SaveButton;
        protected IButton BackButton;
        private Comment comment;

        public int NewsId
        {
            get
            {
                return Globals.ChangeType<int>(ViewState["NewsId"]);
            }
            set
            {
                ViewState["NewsId"] = value;
            }
        }
        public int NewsEntityType
        {
            get
            {
                return Globals.ChangeType<int>(ViewState["NewsEntityType"]);
            }
            set
            {
                ViewState["NewsEntityType"] = value;
            }
        }

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack)
            {
                requestBinders.Add(RequestHelper.BuildGetCommentRequest(this));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            BackButton.Click += new EventHandler(BackButton_Click);
        }
        private void Bind()
        {
            if(comment!=null)
            {
                this.descriptionValuedEditor.Value = comment.Content.Value;
                NewsId = comment.NewsId.Value;
                NewsEntityType = comment.NewsEntityType.Value;
            }   
        }
        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCommentListUrl(NewsId, NewsEntityType));
        }

        #region IReplyConverter<CommentReply,GetOperation> Members

        public void ConvertReply(CommentReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                comment = reply.Entity as Comment;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}